package com.tencreat.service.mall.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.domain.model.LoginUser;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.domain.mall.MallFeedback;
import com.tencreat.domain.mall.dto.MallFeedbackDTO;
import com.tencreat.domain.mall.vo.FeedBackStatisticsVO;
import com.tencreat.mapper.mall.MallFeedbackMapper;
import com.tencreat.service.mall.IMallFeedbackService;
import com.tencreat.service.mall.IMallMessageInformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 意见反馈Service业务层处理
 *
 * @author tencreat
 * @date 2021-08-13
 */
@Service
public class MallFeedbackServiceImpl extends ServiceImpl<MallFeedbackMapper, MallFeedback> implements IMallFeedbackService {

    @Autowired
    private IMallMessageInformService mallMessageInformService;

    /**
     * 查询意见反馈
     *
     * @param id 意见反馈ID
     * @return 意见反馈
     */
    @Override
    public MallFeedback selectMallFeedbackById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询意见反馈列表
     *
     * @param mallFeedback 意见反馈
     * @return 意见反馈
     */
    @Override
    public List<MallFeedback> selectMallFeedbackList(MallFeedback mallFeedback) {
        return baseMapper.selectList(Wrappers.query(mallFeedback));
    }

    /**
     * 新增意见反馈
     *
     * @param mallFeedback 意见反馈
     * @return 结果
     */
    @Override
    public int insertMallFeedback(MallFeedback mallFeedback) {
        mallFeedback.setCreateTime(LocalDateTime.now());
        return baseMapper.insert(mallFeedback);
    }

    /**
     * 修改意见反馈
     *
     * @param mallFeedback 意见反馈
     * @return 结果
     */
    @Override
    public int updateMallFeedback(MallFeedback mallFeedback) {
        mallFeedback.setUpdateTime(LocalDateTime.now());
        mallFeedback.setReplyTime(LocalDateTime.now());
        mallFeedback.setStatus(1);
        mallMessageInformService.addSystemMessage("意见回复","您的意见已回复",mallFeedback.getUserId());
        return baseMapper.updateById(mallFeedback);
    }

    /**
     * 批量删除意见反馈
     *
     * @param ids 需要删除的意见反馈ID
     * @return 结果
     */
    @Override
    public int deleteMallFeedbackByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除意见反馈信息
     *
     * @param id 意见反馈ID
     * @return 结果
     */
    @Override
    public int deleteMallFeedbackById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }

    @Override
    public HttpResult<Boolean> addFeedBack(MallFeedbackDTO mallFeedbackVO, LoginUser loginUser) throws InvocationTargetException, IllegalAccessException {
        MallFeedback feedback = new MallFeedback();
        feedback.setUpdateTime(LocalDateTime.now());
        feedback.setCreateTime(LocalDateTime.now());
        feedback.setUserId(loginUser.getAccountLogin().getUuid());
        feedback.setUsername(loginUser.getAccountLogin().getUsername());
        feedback.setMobile(loginUser.getAccountLogin().getPhone());
        if (mallFeedbackVO.getPicUrls()==null){
            feedback.setHasPicture(0);
        }else {
            feedback.setHasPicture(1);
            feedback.setPicUrls(mallFeedbackVO.getPicUrls());
        }
        feedback.setContent(mallFeedbackVO.getContent());
        return HttpResult.ok(save(feedback));
    }

    @Override
    public HttpResult<List<MallFeedback>> getListByUser(LoginUser loginUser) {
        List<MallFeedback> list = lambdaQuery().eq(MallFeedback::getUserId, loginUser.getAccountLogin().getUuid()).orderByDesc(MallFeedback::getCreateTime).list();
        return HttpResult.ok(list);
    }

    @Override
    public FeedBackStatisticsVO queryFeedBackStatistics(){
        FeedBackStatisticsVO feedBackStatisticsVO = new FeedBackStatisticsVO();
        // 获取今天的时间
        LocalDateTime beginOfDay = LocalDateTimeUtil.beginOfDay(LocalDateTime.now());
        String Day = DateUtil.format(beginOfDay, "yyyyMMdd");

        // 反馈总数
        Integer count = lambdaQuery().count();
        // 已处理总数
        Integer count1 = lambdaQuery().eq(MallFeedback::getStatus, 1).count();
        feedBackStatisticsVO.setTotalCount(count !=null?count:0);
        feedBackStatisticsVO.setAlreadyCount(count1 !=null?count1:0);
        // 未处理
        feedBackStatisticsVO.setNotCount(feedBackStatisticsVO.getTotalCount()-feedBackStatisticsVO.getAlreadyCount());

        // 今日反馈总数
        Integer count2 = lambdaQuery().apply("date_format(create_time,'%Y%m%d') = " + "'" + Day + "'").count();

        // 今日已处理总数
        Integer count3 = lambdaQuery().eq(MallFeedback::getStatus, 1).apply("date_format(reply_time,'%Y%m%d') = " + "'" + Day + "'").count();

        // 今日未处理总数
        Integer count4 = lambdaQuery().eq(MallFeedback::getStatus, 0).apply("date_format(create_time,'%Y%m%d') = " + "'" + Day + "'").count();

        feedBackStatisticsVO.setTodayTotalCount(count2);
        feedBackStatisticsVO.setTodayAlreadyCount(count3);
        feedBackStatisticsVO.setTodayNotCount(count4);
        return feedBackStatisticsVO;
    }
}
